import logging

import pytest

from resource.pom.keyboard import Keyboard
from resource.pom.memorandum import Memorandum


class Test:
    app = Memorandum
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1. 进入桌面，点击备忘录，新增笔记')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.add_note)
        logging.info('预期结果1.可正常新增笔记')
        device.refresh_layout()
        device.assert_text_exist('标题')
        device.assert_type_exist('Web')
        title = device.get_element_by_text('标题')
        web = device.get_element_by_type('Web')

        device.click_element(title)
        device.refresh_layout()
        title_editor = device.get_element_by_condition(condition={'type': 'TextInput', 'hint': '名称'})
        device.click_element(title_editor)
        device.wait(0.5)
        device.click(*self.keyboard.key_q)
        device.click(*self.keyboard.key_w)
        device.click(*self.keyboard.key_e)
        device.click(*self.keyboard.close)
        device.click_element(device.get_element_by_text('保存'))

        device.click_element(web)
        device.wait(0.5)
        device.click(*self.keyboard.key_r)
        device.click(*self.keyboard.key_t)
        device.click(*self.keyboard.key_y)
        device.click(*self.keyboard.close)
        device.click(*self.app.save_note)
        device.refresh_layout()
        device.assert_text_exist('qwe')
        # device.assert_text_exist('rty')

        logging.info('步骤2. 删除笔记')
        device.click(*self.app.del_note)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('删除'))
        logging.info('预期结果2. 最近删除中显示对应已删除的笔记')
        device.click(*self.app.open_menu)
        device.refresh_layout()
        assert device.is_element_by_text_in_area('1', 200, 1140, 280, 1180), '菜单页最近删除中笔记数量不对'
        device.click(*self.app.recent_delete)
        device.click(*self.app.close_menu)
        device.refresh_layout()
        device.assert_text_exist('qwe')

        logging.info('步骤3.进入最近删除，点击笔记，点击恢复按钮')
        device.click_element(device.get_element_by_text('qwe'))
        device.click(*self.app.recover_from_dustbin)
        device.go_back()
        logging.info('预期结果3.笔记恢复至原来的位置，最近删除中笔记数量相应减少')
        device.refresh_layout()
        device.assert_text_exist('qwe', expect=False)
        device.click(*self.app.open_menu)
        device.refresh_layout()
        assert device.is_element_by_text_in_area('0', 200, 1140, 280, 1180), '菜单页最近删除中笔记数量不对'
        device.click(*self.app.all_note)
        device.click(*self.app.close_menu)
        device.refresh_layout()
        device.assert_text_exist('qwe')
